/**
 * 测试问卷功能
 */

var is_exist_key = null;
var answer = null;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	init_bind();

	// 若是查看结果，则显示已经选择的选项
	if( !empty( answer ) )
	{
		init_answer( answer );
	}
} );

//--------------------------------------------------------------------------
//
//	method
//
//--------------------------------------------------------------------------

function init_bind()
{
	// 若是查看问卷结果，则隐藏提交按钮
	if( is_exist_key )
	{
		submit_success();
	}
	else
	{
		// 绑定提交方法
		$( "#btnSubmit" ).click( submit_form );

		// 所有自定义选项，点击输入框自动选定
		$( "#vote_list .right > label > input[type='text']" ).click( customInputHandler );

		// 移除可能存在的未选择提示样式
		$( "#vote_list .right" ).each( function( idx )
		{
			$( this ).find( "textarea" ).add( $( this ).find( "input" ) ).click( {
				tar: $( this ).parent()
			}, inputClickHandler );
		} );
	}
}

function init_answer( value )
{
	for( var ns in value )
	{
		var id = ns;
		var d = value[ ns ];
		var items = d.items;
		var vs;

		$( "#" + id ).find( "ul li" ).each( function( j )
		{
			if( items[ j ] instanceof Array )
			{
				// 多选
				for( var i = 0; i < items[ j ].length; i++ )
				{
					vs = items[ j ][ i ].split( ":" );
					
					var checkbox = $( this ).find( ".right :checkbox" ).eq( vs[ 0 ] );
					checkbox.prop( "checked", true );

					if( checkbox.val() == "自定义" )
						checkbox.parent().find( ":text" ).val( vs[ 1 ] );
				}
			}
			else
			{
				vs = items[ j ].split( ":" );

				if( $( this ).find( ".right textarea" ).length > 0 )
				{
					// 输入框
					$( this ).find( ".right textarea" ).val( vs[ 1 ] );
				}
				else
				{
					// 单选
					var radio = $( this ).find( ".right :radio" ).eq( vs[ 0 ] );
					radio.prop( "checked", true );

					if( radio.val() == "自定义" )
						radio.parent().find( ":text" ).val( vs[ 1 ] );
				}
			}
		} );
	}
}

//--------------------------------------------------------------------------
//
//	submit
//
//--------------------------------------------------------------------------

function submit_form()
{
	// 若是查看问卷结果，则不需要提交
	if( is_exist_key )
		return;

	var data = {};

	// 测评选项
	var answer = collet_data();
	data.answer = JSON.stringify( answer );

	if( answer == null )
		return false;

	// 附加标识信息
	data.key = $( "#key" ).val();
	data.ask = $( "#ask" ).val();

	// 联系信息
	data.nickname = $( "#nickname" ).val();
	data.phone = $( "#phone" ).val();

	submitter.post( "vote_go.php", data, function( d )
	{
		if( d.err == 0 )
		{
			jalert( d.msg, "提示信息" );

			// 缓存提交成功返回的唯一标识号
			$( "#key" ).val( d.key );

			// 收起提交表单，并且隐藏，以免重复提交
			submit_success( d );
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

function submit_success()
{
	$( "#btnSubmit" ).hide();
	$( "#vote_form input" ).prop( "readonly", true );
	$( "#vote_list .right input" ).prop( "readonly", true );
	$( "#vote_list .right textarea" ).prop( "readonly", true );
	$( "#vote_list .right :radio" ).prop( "disabled", true );
	$( "#vote_list .right :checkbox" ).prop( "disabled", true );

	$( "#vote_list .right > label > input[type='text']" ).unbind( "click" );
}

// 收集测评选择数据
function collet_data()
{
	var a = {};
	var err = false;

	$( "#vote_list .item_child" ).each( function( i )
	{
		var id = $( this ).attr( "id" );
		var topic = $( this ).find( "p.topic" ).text();
		var items = [];// 选项列表，直接存储值数据

		$( this ).find( "ul li" ).each( function( j )
		{
			var right = $( this ).find( ".right" );
			var value = "";

			if( right.find( "textarea" ).length > 0 )
			{
				// 选填内容
				value = "0:" + right.find( "textarea" ).val();

				if( empty( right.find( "textarea" ).val() ) )
				{
					value = "-1:undefined";
				}
			}
			else if( right.find( ":checkbox" ).length > 0 )
			{
				value = "-1:undefined";

				// 多选
				right.find( ":checked" ).each( function( idx, el )
				{
					if( value == "-1:undefined" )
						value = [];

					var index = getIndex( $( el ), right.find( ":checkbox" ) );

					if( $( el ).val() == "自定义" )
					{
						value.push( index + ":" + $( el ).parent().find( ":text" ).val() );
					}
					else
					{
						value.push( index + ":" + $( el ).val() );
					}
				} );
			}
			else
			{
				// 单选
				var index = getIndex( right.find( ":checked" ), right.find( ":radio" ) );

				if( right.find( ":checked" ).val() == "自定义" )
				{
					value = index + ":" + right.find( ":checked" ).parent().find( ":text" ).val();
				}
				else
				{
					value = index + ":" + right.find( ":checked" ).val();
				}
			}

			if( empty( value ) || value == "-1:undefined" )
			{
				infopad.err( "调查表单未完成！" );
				$( window ).scrollTop( $( this ).find( ".left" ).position().top - 50 );
				$( this ).addClass( "unselect" );

				err = true;
				return false;
			}

			items.push( value );
		} );

		if( err )
		{
			return false;
		}

		a[ id ] = {
			id: id,
			topic: topic,
			items: items
		};
	} );

	if( err )
	{
		return null;
	}

	return a;
}

function getIndex( tar, group )
{
	var index = -1;

	group.each( function( idx )
	{
		if( tar.val() == $( this ).val() )
		{
			index = idx;
			return false;
		}
	} );

	return index;
}

//--------------------------------------------------------------------------
//
//	event
//
//--------------------------------------------------------------------------

function inputClickHandler( e )
{
	e.data.tar.removeClass( "unselect" );
}

function customInputHandler( e )
{
	// 选定当前选项
	$( this ).parent().find( ":checkbox" ).prop( "checked", true );
	$( this ).parent().find( ":radio" ).prop( "checked", true );
}

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
}
